<?php
class LicencesController extends AppController {
	var $uses = array('User','Licence','Constants');
	
	public function beforeFilter() {
		$this->Auth->deny('add');
	}
	
    public function addBySecretaire($userId=0,$club, $type) {
		if ($userId != 0) {
			
			$this->Licence->create();
			$this->request->data['Licence']['user_id']=$userId;
			$this->request->data['Licence']['status']='validated';
			$this->request->data['Licence']['year']=2014;
			$this->request->data['Licence']['club']=$club;
			$this->request->data['Licence']['type']=$type;
			$this->Licence->save($this->request->data);
		}
	}
	
	public function add() {
		if ($this->request->is('post')) {
			$this->Licence->create();
			$this->request->data['Licence']['status']='requested';
			$this->request->data['Licence']['year']=2014;
			$this->request->data['Licence']['club']=$this->Session->read('Auth.User.club') ;
			
			if ($this->Licence->save($this->request->data)) {
				$this->Session->setFlash(__('Votre demande de licence a été enregistrée !'));
				$this->redirect('/welcome/viewMyDossier');
			} else {
				$this->Session->setFlash(__('Votre enregistrement a échoué, contactez l\'administrateur du site.'));
			}
		}
	}

    public function valider($licenceId) {
       $licence['Licence'] = array('id'=>$licenceId,"status"=>"validated");
       $this->Licence->save($licence);
       $this->Session->setFlash(__("La demande de licence numéro $licenceId a été validée !"));
       $this->redirect('/ManageMembers/licence');
    }
    
    public function refuser($licenceId) {
       $licence['Licence'] = array('id'=>$licenceId,"status"=>"refuser"); 
       $this->Licence->save($licence);
       $this->Session->setFlash(__("La demande de licence numéro $licenceId a été refusée !"));
       $this->redirect('/ManageMembers/licence');
    }
    
    public function produced($licenceId) {
       $licence['Licence'] = array('id'=>$licenceId,"status"=>"produced"); 
       $this->Licence->save($licence);
       $this->Session->setFlash(__("La demande de licence numéro $licenceId a été produite !"));
       $this->redirect('/ManageLicences/licence');
    }
    
  public function requestAndValidateLicences(){
  	$data=$this->data;
  	if($this->isManageAutorisationAllowed()){
  		$users=$this->User->find('all',array('fields'=>array('User.id','User.club')));
	  	foreach($users as $user){
	  		if($data['tourism'][$user['User']['id']]==1){
	  			$this->addBySecretaire($user['User']['id'],$user['User']['club'],'Tourisme');
	  		}
	  		if($data['competition'][$user['User']['id']]==1){
	  			$this->addBySecretaire($user['User']['id'],$user['User']['club'],'Competition');
	  		}
	  	}
  	}
  	else{
	  	$club=$this->Session->read('Auth.User.club');
	  	$users=$this->User->find('all',array('fields'=>array('User.id'),'conditions'=>array('club'=>$club)));
	  	foreach($users as $user){
	  		if($data['tourism'][$user['User']['id']]==1){
	  			$this->addBySecretaire($user['User']['id'],$club,'Tourisme');
	  		}
	  		if($data['competition'][$user['User']['id']]==1){
	  			$this->addBySecretaire($user['User']['id'],$club,'Competition');
	  		}
	  	}
  	}
  	
  	$this->redirect('/ManageMembers/licence');
  	
  	
  }
    
    
    
    
    
}
?>